﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CopySniffer.UI.Core.FileLoad
{
    public static class FileLoaders
    {
        static FileLoaders()
        {
            var q = from loader in GetLoaders()
                    from extension in loader.SupportedExtensions
                    select new { extension, loader };

            loadersByExtension = q.ToDictionary(e => e.extension, e => e.loader);
        }

        private static List<IFileLoader> loaders;
        private static Dictionary<string, IFileLoader> loadersByExtension;

        private static List<IFileLoader> GetLoaders()
        {
            loaders = new List<IFileLoader>()
            {
                new TextFileLoader(),
                new PdfFileLoader(),
                new GeneralFileLoader()
            };

            return loaders;
        }

        public static IEnumerable<IFileLoader> All
        {
            get
            {
                return loaders.AsEnumerable();
            }
        }

        public static IFileLoader GetFileLoader(string fileName)
        {
            IFileLoader loader = null;
            if (! loadersByExtension.TryGetValue(
                Path.GetExtension(fileName).Substring(1), 
                out loader))
            {
                loader = loadersByExtension["*"];
            }
            return loader;
        }
    }
}
